
buildPath = ../../.build/
dependPath = ../../script/depend/
gcc = /opt/Xilinx/SDK/2015.4/gnu/arm/lin/bin/arm-xilinx-linux-gnueabi-gcc
g++ = /opt/Xilinx/SDK/2015.4/gnu/arm/lin/bin/arm-xilinx-linux-gnueabi-g++

app:
	cd ../applications && $(gcc) -o server.cgi server.c ../cgic/cgic.c -I../cgic
	cd ../applications && $(g++) -o regrw Reg.cpp regrw.cpp ../utils/StrToInt.cpp -I./ -I../utils
	cd ../applications && $(g++) -o platform platform.cpp ../utils/StrToInt.cpp -I./ -I../utils -lpthread

bin:
	cp -f $(dependPath)/arm_ramdisk/uramdisk.image.gz $(buildPath)/rootfs

copy:
	cd ../cgic && make
	rm -rf $(dependPath)/arm_ramdisk/tmp_mnt/var/www
	cp -raf * $(dependPath)/arm_ramdisk/tmp_mnt/
	cp ../cgic/libcgic.a $(dependPath)/arm_ramdisk/tmp_mnt/lib
	mkdir  $(dependPath)/arm_ramdisk/tmp_mnt/var/www/cgi-bin
	cp ../cgic/capture            $(dependPath)/arm_ramdisk/tmp_mnt/var/www/cgi-bin/
	cp ../cgic/cgictest.cgi       $(dependPath)/arm_ramdisk/tmp_mnt/var/www/cgi-bin/
	mv ../applications/server.cgi $(dependPath)/arm_ramdisk/tmp_mnt/var/www/cgi-bin/
	mv ../applications/platform   $(dependPath)/arm_ramdisk/tmp_mnt/usr/sbin/
	mv ../applications/regrw      $(dependPath)/arm_ramdisk/tmp_mnt/usr/sbin/
	cd ../cgic && make clean

build:
	make app
	cd $(dependPath)/arm_ramdisk && gunzip arm_ramdisk.image.gz
	cd $(dependPath)/arm_ramdisk && chmod u+rwx arm_ramdisk.image
	cd $(dependPath)/arm_ramdisk && mkdir tmp_mnt
	cd $(dependPath)/arm_ramdisk && sudo mount -o loop arm_ramdisk.image tmp_mnt/
	make copy
	cd $(dependPath)/arm_ramdisk && sudo umount tmp_mnt/
	cd $(dependPath)/arm_ramdisk && gzip arm_ramdisk.image
	cd $(dependPath)/arm_ramdisk && mkimage -A arm -T ramdisk -C gzip -d arm_ramdisk.image.gz uramdisk.image.gz
	make bin

remove:
	cd $(dependPath) && rm -rf arm_ramdisk
	cd $(buildPath)  && rm -rf rootfs

new:
	make remove
	cd $(dependPath) && mkdir arm_ramdisk && cp -f arm_ramdisk.image.gz arm_ramdisk
	cd $(buildPath)  && mkdir rootfs
	make build
    
.PHONY:clean